package com.x.erp.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.x.erp.model.Feedback;
import com.x.framework.dao.BaseDao;

/**
 * 
 * 用户反馈信息dao
 * 
 * @author pansong
 *
 */
@Repository
public class FeedbackDao extends BaseDao {

	private final static String selectFeedbackListSql = "SELECT " + "ef.*, " + "eu.USER_NAME, " + "eu.USER_MOBILE "
			+ "FROM " + "ERP_FEEDBACK ef  " + "JOIN ERP_USER eu ON ef.USER_ID = eu.USER_ID " + "WHERE 1=1 ";

	/**
	 * 查询预订列表
	 * 
	 * @param feedback
	 * @return
	 * @throws Exception
	 */
	public List<Feedback> selectFeedbackList(Feedback feedback) throws Exception {
		StringBuilder sql = new StringBuilder(selectFeedbackListSql);
		List<Object> params = new ArrayList<Object>();
		searchCondition(sql, params, feedback);
		sql.append("ORDER BY ef.FEEDBACK_CREATE_TIME DESC ");
		return this.queryForList(sql.toString(), Feedback.class, params, feedback);
	}

	/**
	 * 查询条件
	 * 
	 * @param sql
	 * @param params
	 * @param feedback
	 */
	private void searchCondition(StringBuilder sql, List<Object> params, Feedback feedback) {
		if (feedback != null) {
			if (StringUtils.isNotEmpty(feedback.getFeedbackId())) {
				sql.append("AND ef.FEEDBACK_ID = ? ");
				params.add(feedback.getFeedbackId());
			}
			if (StringUtils.isNotEmpty(feedback.getUserId())) {
				sql.append("AND ef.USER_ID = ? ");
				params.add(feedback.getUserId());
			}
			if (feedback.getFeedbackCreateTime() != null) {
				sql.append("AND DATE_FORMAT(ef.FEEDBACK_CREATE_TIME,'%Y-%m-%d') = DATE_FORMAT(?,'%Y-%m-%d') ");
				params.add(feedback.getFeedbackCreateTime());
			}
		}
	}

	private final static String selectFeedbackNumSql = "SELECT COUNT(ef.FEEDBACK_ID)  FROM ERP_FEEDBACK ef WHERE 1 = 1 ";

	/**
	 * 获取用户反馈记录总数
	 * 
	 * @param sql
	 * @param params
	 * @param feedback
	 */
	public int selectFeedbackNum(Feedback feedback) throws Exception {
		StringBuilder sql = new StringBuilder(selectFeedbackNumSql);
		List<Object> params = new ArrayList<Object>();
		searchCondition(sql, params, feedback);
		return this.queryForInt(sql.toString(), params);
	}
}
